home *** CD-ROM | disk | FTP | other *** search
/ Plug-In Power Pack for Netscape Communicator / Plug-In Power Pack for Netscape Communicator.iso / plugins / dataviews / dvtools / examples / programs / dragging.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-05-08  |  11.4 KB  |  358 lines

  1. #ifndef    lint
  2. static char SccsId[]= "@(#)dragging.c    V1.21    3/22/95";
  3. #endif
  4. /*
  5. |    file name - dragging.c
  6. |===================================================================
  7. |
  8. |    This example uses XORing to "drag" objects around. Select an
  9. |    object with the LEFT or MIDDLE mouse button, drag the object
  10. |    by holding the mouse button DOWN, when you release the button
  11. |    the object stays in the new position.
  12. |
  13. |    To quit the program select "q|Q" or the RIGHT mouse button.
  14. |
  15. |===================================================================
  16. */
  17. #include <windows.h>
  18.  
  19. /* DV-Tools header files */
  20. #include "std.h"            /* <stdio.h> etc., scalar & macro definitions */
  21. #include "dvstd.h"          /* public types & constants */
  22. #include "dvtools.h"        /* constants used by T routines */
  23. #include "dvGR.h"           /* constants used by window mgt & GR routines */
  24. #include "VOstd.h"          /* constants used by VO & VOob routines */
  25. #include "Tfundecl.h"       /* T routines (screens, drawports & views) */
  26. #include "VOfundecl.h"      /* VO routines (objects) */
  27. #include "VUerfundecl.h"    /* VUer routines (event handling routines) */
  28. #include "GRfundecl.h"      /* GR routines (interface to display device) */
  29.  
  30.  
  31. /* Constants */
  32. #define  DVPATH            (char *)NULL
  33. #define  DISPFORMS_STB     (char *)NULL
  34. #define  DVDEVICE          (char *)NULL
  35. #define  DVCOLORTABLE      (char *)NULL
  36. #define  VIEW_NAME         "move_objs.v"
  37. #define  SCREEN_VIEWPORT   (RECTANGLE *)NULL
  38. #define  DRAWING_VIEWPORT  (RECTANGLE *)NULL
  39.  
  40. /* Define global variables */
  41. DRAWPORT drawport;         /* how & where to display picture, picture frame */
  42. OBJECT move_object;        /* object selected to be moved */
  43.  
  44.  
  45. /* Global forward function declarations */
  46. void MoveObject V_P_((DV_POINT *source, DV_POINT *dest));
  47.  
  48.  
  49. /*
  50.  *   MAIN PROGRAM
  51.  */
  52. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  53.                      LPSTR lpCmdLine, int nCmdShow )
  54. {
  55.   INT argc = 0;
  56.   CHAR **argv;
  57.  
  58.   /*
  59.    *  program arguments
  60.    *    argv[1] - display device name (default is to use DVDEVICE)
  61.    *    argv[2] - view name (default is move_objs.v)
  62.    */
  63.  
  64.   /* Define & initialize device name and view filename */
  65.   char *device_name = DVDEVICE; /* default device name */
  66.   char *view_name = VIEW_NAME;  /* default view name */
  67.  
  68.   /* Define display variables */
  69.   OBJECT screen;                /* display device, the window */
  70.   VIEW view;                    /* picture representation of the view file */
  71.  
  72.   /* Control loop variables */
  73.   OBJECT location;              /* the event representation */
  74.  
  75.   /* Variables used for rubberbanding */
  76.   DV_POINT src_pt,              /* start point or current point of object */
  77.            dest_pt;             /* position to move to */
  78.   DV_POINT *world_pt;           /* world coordinate point */
  79.   int rubberbanding = NO,       /* flag for rubberbanding */
  80.       Quit = NO;                /* flag to quit program */
  81.  
  82.   /*-----------------
  83.    *   Initialization
  84.    *
  85.    *   TInit:    perform the initialization of DV-Tools
  86.    *             TInit reads your configuration file and any
  87.    *             environment variables or logical names set.
  88.    */
  89.   make_argv(&argc,&argv,GetCommandLine());
  90.   TInit( DVPATH, DISPFORMS_STB );
  91.  
  92.   /*
  93.    *   TscOpenSet:  opens a device as a screen object using
  94.    *                specified attributes
  95.    *
  96.    *   Set exposure block to YES to insure the window
  97.    *   is ready for drawing when TdpDraw is called.
  98.    */
  99.   if (argc > 1)
  100.     device_name = argv[1];
  101.   screen = TscOpenSet (device_name, DVCOLORTABLE,
  102.                        V_WINDOW_NAME, "XOR example",
  103.                        V_WINDOW_WIDTH, 600,
  104.                        V_WINDOW_HEIGHT, 600,
  105.                        V_X_EXPOSURE_BLOCK, YES,
  106.                        V_ACTIVE_CURSOR, V_END_OF_LIST);
  107.   if (!screen)
  108.     {
  109.       printf ("Must specify device on command line or");
  110.       printf (" in DataViews configuration file.\n");
  111.       S_EXIT (EXIT_ERR);
  112.     }
  113.  
  114.   /*
  115.    *   VOscWinEventMask:  sets the screen's window event mask
  116.    */
  117.   VOscWinEventMask ((ULONG) V_KEYPRESS | V_BUTTONPRESS | V_BUTTONRELEASE
  118.                 | V_MOTIONNOTIFY | V_EXPOSE | V_RESIZE,
  119.             (ULONG) 0);
  120.  
  121.   /*
  122.    *   TviLoad:   Load a view in from a file, either a
  123.    *              user supplied view or default view file move_objs.v
  124.    *   TdpCreate: Create a DV-Tools window, a drawport.
  125.    *              The drawport is attached to the screen object
  126.    *              specified while view specifies the view to be
  127.    *              displayed on the screen.
  128.    */
  129.   if (argc == 3)
  130.     view_name = argv[2];
  131.   view = TviLoad (view_name);
  132.   if (!view)
  133.     {
  134.       printf ("Could not load view from file ");
  135.       printf ("%s.\n", view_name);
  136.       S_EXIT (EXIT_ERR);
  137.     }
  138.   drawport = TdpCreate (screen, view, SCREEN_VIEWPORT, DRAWING_VIEWPORT);
  139.  
  140.   /*
  141.    *   TscErase: Erase the entire screen in the default
  142.    *             background color
  143.    *   TdpDraw:  Draw the contents of the drawport
  144.    */
  145.   TscErase (screen);
  146.   TdpDraw (drawport);
  147.  
  148.   /*--------------------
  149.    *   Control loop
  150.    *
  151.    *   Poll the event queue for window events. If the key
  152.    *   represents the character 'q' or 'Q' then quit the program.
  153.    *   If user selects to drag an object using the left or middle
  154.    *   mouse buttons then wait for the buttonrelease before
  155.    *   dropping the object.
  156.    */
  157.   FOREVER
  158.   {
  159.     location = VOscWinEventPoll (V_WAIT);
  160.     switch (VOloType (location))
  161.       {
  162.  
  163.       case V_EXPOSE:
  164.         /*
  165.          *  VOloRegion:  Returns a rectangle representing the
  166.          *               exposed region on the screen.
  167.          *  TscRedraw:   After erasing, redraws all the drawports
  168.          *               in the screen.
  169.          *  A portion of the window has been exposed and needs
  170.          *  to be redrawn.
  171.          */
  172.         TscRedraw (screen, VOloRegion (location));
  173.         break;
  174.  
  175.       case V_RESIZE:
  176.         /*
  177.          *  The window size has been changed.
  178.          *  TscReset:  Resets all screen drawports after
  179.          *             window resizing
  180.          */
  181.         TscReset (screen);
  182.         break;
  183.  
  184.       case V_KEYPRESS:
  185.         /*
  186.          *  Check key selected.
  187.          *  VOloKeySym:  Returns the key symbol value of the
  188.          *               location object
  189.          *  If the key symbol represents the characters 'q'
  190.          *  or 'Q' then quit the program.
  191.          */
  192.         switch (VOloKeySym (location))
  193.           {
  194.           case 'q':
  195.           case 'Q':
  196.             Quit = YES;
  197.             break;
  198.  
  199.           default:
  200.             break;
  201.           }
  202.         break;
  203.  
  204.       case V_BUTTONPRESS:
  205.         /*
  206.          *  Check button selected.
  207.          *  VOloButton:  Returns the button that was pressed
  208.          *  TloGetSelectedObject: Get the selected object
  209.          *
  210.          *  The left/middle mouse button acts as the selection
  211.          *  button. The object selected will be moved to the
  212.          *  mouse position designated by the Buttonrelease.
  213.          */
  214.         switch (VOloButton (location))
  215.           {
  216.           case 1:
  217.             if (!rubberbanding)
  218.               {
  219.                 if ((move_object = TloGetSelectedObject (location)))
  220.                   {
  221.                     /*
  222.              *  VOloWcpGet:  Returns the location object in drawing's
  223.              *                 world coordinates.
  224.              *  GRset:         Resets device attributes
  225.              *
  226.              *  Erase the selected object, obtain the point information
  227.              *  used to select the object and set the drawing mode
  228.              *  to XOR. XOR draws by reversing bits and is applicable
  229.              *  for rubberbanding. Finally, draw the object with the
  230.              *  current drawing mode and set the rubberbanding flag
  231.              *  to YES.
  232.              */
  233.                     world_pt = VOloWcpGet (location);
  234.                     if (world_pt != NULL)
  235.                       {
  236.                         TdpEraseObject (drawport, move_object);
  237.                         src_pt.x = world_pt->x;
  238.                         src_pt.y = world_pt->y;
  239.                         dest_pt.x = src_pt.x;
  240.                         dest_pt.y = src_pt.y;
  241.                         GRset (V_DRAW_FUNCTION, V_XOR, V_END_OF_LIST);
  242.                         TdpDrawObject (drawport, move_object);
  243.                         rubberbanding = YES;
  244.                       }
  245.                   }
  246.               }
  247.             break;
  248.  
  249.           case 3:
  250.             Quit = YES;
  251.             break;
  252.           }
  253.         break;
  254.  
  255.       case V_BUTTONRELEASE:
  256.         /*
  257.          *  GRset:        Resets device attributes
  258.          *  TdpDrawObject:  Draw an object in a drawport
  259.          *
  260.          *  Reset the drawing mode to V_COPY which represents the
  261.          *  normal drawing mode.  Draw the object in it's current
  262.          *  position using the current drawing mode. Set the
  263.          *  the rubberbanding flag to NO.
  264.          */
  265.         if (rubberbanding == YES)
  266.           {
  267.             GRset (V_DRAW_FUNCTION, V_COPY, V_END_OF_LIST);
  268.             TdpDrawObject (drawport, move_object);
  269.             rubberbanding = NO;
  270.           }
  271.         break;
  272.  
  273.       case V_MOTIONNOTIFY:
  274.         /*
  275.          *  VOloWcpGet:  Returns the location object in drawing's
  276.          *           world coordinates.
  277.          *
  278.          *  Make the previous point be the source point and copy
  279.          *  the current point to the destination point. Then call
  280.          *  the procedure to move the object.
  281.          */
  282.         if (rubberbanding == YES)
  283.           {
  284.             world_pt = VOloWcpGet (location);
  285.             if (world_pt != NULL)
  286.               {
  287.                 src_pt.x = dest_pt.x;
  288.                 src_pt.y = dest_pt.y;
  289.                 dest_pt.x = world_pt->x;
  290.                 dest_pt.y = world_pt->y;
  291.  
  292.                 MoveObject (&src_pt, &dest_pt);
  293.               }
  294.           }
  295.         break;
  296.  
  297.       default:
  298.         break;
  299.       }
  300.  
  301.     if (Quit == YES)
  302.       break;
  303.   }
  304.  
  305.   /*--------------------
  306.    *   Termination
  307.    *
  308.    *   TscErase:               Erase the entire screen in the default
  309.    *                           background color
  310.    *   TdpDestroy:             Destroy the drawport,
  311.    *   TviDestroy:             Destroy the view, freeing the allocated memory
  312.    *   TscCloseCurrentScreen:  Close the current display screen
  313.    *   TTerminate:             Perform the clean-up for DV-Tools
  314.    */
  315.   TscErase (screen);
  316.   TdpDestroy (drawport);
  317.   TviDestroy (view);
  318.   TscCloseCurrentScreen ();
  319.   TTerminate ();
  320.   return EXIT_OK;
  321. }
  322.  
  323. void 
  324. MoveObject (source, dest)
  325.      DV_POINT *source;
  326.      DV_POINT *dest;
  327. {
  328.   OBJECT point;
  329.   int i, delta_x, delta_y;
  330.  
  331.   /*
  332.    *  Determine the change between the start point and
  333.    *  the current destination point. This is the amount
  334.    *  that each control point belonging to the object
  335.    *  will be moved.
  336.    */
  337.   delta_x = dest->x - source->x;
  338.   delta_y = dest->y - source->y;
  339.  
  340.   /*
  341.    *  TdpDrawObject:  Draws an object in a drawport
  342.    */
  343.   TdpDrawObject (drawport, move_object);
  344.  
  345.   /*
  346.    *  VOobPtGet: Gets the specified control point of an object
  347.    *  VOptMove:  Moves a point object
  348.    *
  349.    *  Get each control object and move it by the calculated amount.
  350.    */
  351.   for (i = 1; i <= VOobPtGet (move_object, 0); i++)
  352.     {
  353.       point = VOobPtGet (move_object, i);
  354.       VOptMove (point, DV_RELATIVE, delta_x, delta_y);
  355.     }
  356.   TdpDrawObject (drawport, move_object);
  357. }
  358.